<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车</title>
    <link rel="stylesheet" href="./css/style.css">
    <link rel="stylesheet" href="./css/product-list.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f4f4f4;
        }

        header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 10px 20px;
            background-color: #333;
            color: white;
        }

        header h1 {
            margin: 0;
        }

        header nav a {
            color: white;
            text-decoration: none;
            padding: 10px 15px;
            border-radius: 5px;
        }

        header nav a:hover {
            background-color: #555;
        }

        #cart {
            margin-top: 20px;
        }

        .table-cart {
            width: 100%;
            border-collapse: collapse;
        }

        .table-cart th,
        .table-cart td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }

        .table-cart th {
            background-color: #f2f2f2;
        }

        .cart-summary {
            margin-top: 20px;
            text-align: right;
        }

        .cart-summary p {
            font-size: 18px;
            font-weight: bold;
        }

        .cart-summary button {
            background-color: #28a745;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin-left: 10px;
        }

        .cart-summary button:hover {
            background-color: #218838;
        }

        #discount-code {
            margin-top: 20px;
            display: flex;
            justify-content: flex-end;
            align-items: center;
        }

        #discount-code input {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }

        #discount-code button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin-left: 10px;
        }

        #discount-code button:hover {
            background-color: #0056b3;
        }

        .dropdown {
            position: relative;
            display: inline-block;
        }

        .dropdown-content {
            display: none;
            position: absolute;
            background-color: #f9f9f9;
            min-width: 160px;
            box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
            z-index: 1;
        }

        .dropdown-content a {
            color: black;
            padding: 12px 16px;
            text-decoration: none;
            display: block;
        }

        .dropdown-content a:hover {
            background-color: #f1f1f1;
        }

        .dropdown:hover .dropdown-content {
            display: block;
        }

        .dropdown:hover .dropbtn {
            background-color: #555;
        }
    </style>
</head>

<body>
    <header>
        <h1>我的购物车</h1>
        <nav>
            <a href="index.html">首页</a>
            <div class="dropdown">
                <a href="javascript:void(0);">分类</a>
                <div class="dropdown-content">
                    <a href="computer-pad.html">电脑平板</a>
                    <a href="earphone.html">耳机</a>
                    <a href="sound.html">音响</a>
                    <a href="router.html">智能家居</a>
                    <a href="phone.html">手机</a>
                    <a href="smart.html">智能数码</a>
                </div>
            </div>
            <a href="user-center.html">用户中心</a>
        </nav>
    </header>

    <div id="cart">
        <div id="cart-items">
            <table class="table-cart">
                <thead>
                    <tr>
                        <th>产品名称</th>
                        <th>价格（元）</th>
                        <th>数量</th>
                        <th>小计（元）</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody id="cart-items-body">
                    <!-- 动态生成内容 -->
                </tbody>
            </table>
        </div>
        <div class="cart-summary" id="cart-summary">
            <p>总计：<span id="total-price">0</span>元</p>
            <button onclick="clearCart()">清空购物车</button>
            <button onclick="checkout()">结算</button>
        </div>
    </div>

    <div id="discount-code">
        <input type="text" id="code" placeholder="输入优惠码">
        <button onclick="applyDiscount()">应用</button>
    </div>

    <script>
        let discount = 1; // 折扣比例，默认为1（无折扣）

        function loadCart() {
            let cart = JSON.parse(localStorage.getItem('cart')) || [];
            const cartItemsBody = document.getElementById('cart-items-body');
            cartItemsBody.innerHTML = '';
            let totalPrice = 0;

            cart.forEach((item, index) => {
                const cartItemRow = document.createElement('tr');
                cartItemRow.innerHTML = `
                    <td>${item.productName}</td>
                    <td>${item.price.toFixed(2)}</td>
                    <td>
                        <button onclick="changeQuantity(${index}, -1)">-</button>
                        ${item.quantity}
                        <button onclick="changeQuantity(${index}, 1)">+</button>
                    </td>
                    <td>${(item.price * item.quantity * discount).toFixed(2)}</td>
                    <td><button onclick="removeItem(${index})">删除</button></td>
                `;
                totalPrice += item.price * item.quantity * discount;
                cartItemsBody.appendChild(cartItemRow);
            });

            document.getElementById('total-price').innerText = totalPrice.toFixed(2);
        }

        function changeQuantity(index, change) {
            let cart = JSON.parse(localStorage.getItem('cart')) || [];
            cart[index].quantity += change;

            if (cart[index].quantity < 1) {
                cart[index].quantity = 1; // 最少为1
            }

            localStorage.setItem('cart', JSON.stringify(cart));
            loadCart(); // 重新加载购物车
        }

        function removeItem(index) {
            let cart = JSON.parse(localStorage.getItem('cart')) || [];
            cart.splice(index, 1);
            localStorage.setItem('cart', JSON.stringify(cart));
            loadCart(); // 重新加载购物车
        }

        function clearCart() {
            localStorage.removeItem('cart');
            loadCart(); // 重新加载购物车
        }

        function checkout() {
            alert('结算成功！');
            clearCart(); // 清空购物车
        }

        function applyDiscount() {
            let code = document.getElementById('code').value;

            if (code === 'DISCOUNT10') { // 示例优惠码
                discount = 0.9; // 10% 折扣
                alert('优惠码应用成功！');
            } else {
                discount = 1; // 无折扣
                alert('无效的优惠码！');
            }

            loadCart(); // 重新加载购物车以应用折扣
        }

        // 页面加载完成后立即加载购物车内容
        window.onload = loadCart;
    </script>
</body>

</html>